/* In each year, match newly eligible enrollees to eligible enrollees who also enrolled 
last year, and compare subsidized and unsubsidized premiums
*/

cap log close
set more off
clear
cap restore, not

log using descriptive_inertia_tabfig.txt, text replace

insheet using ca_choice_data_for_ashley.csv, comma clear names
isid plan_name household_id year
save choice, replace

insheet using ca_household_data_for_ashley.csv, comma clear names
isid household_id year
save household, replace

insheet using ca_plan_data_for_ashley.csv, comma clear names
isid plan_name
save plan, replace

** Match new enrollees in each year t to enrollees in t that had also enrolled in t-1
local matchlist "slc_contribution rating_factor perc* num_choices"	

forval y=2015/2018 {

use household, clear
gsort household_id year
by household_id: gen outprev=out_of_market[_n-1]
keep if year==`y' & out_of_market==0 & plan_number!=141
gen new=outprev==1
drop if new==0 & previous_plan_number=="NA"

* Focus on individuals
keep if weight==1

gen sort=uniform()									// Random sort (necessary for psmatch2 to work well).
sort sort
drop sort

gen match_pscore=0									// Variable that will tag NEW obs. and those that match them.
replace match_pscore=1 if new==1
label var match_pscore "Propensity score matching on levels"

psmatch2 new `matchlist', neighbor(1) noreplacement

tempfile tm
save `tm'

keep if new==1
preserve
keep _n1
rename _n1 _id
joinby _id using `tm' 
tempfile neighbors
save `neighbors'
restore
append using `neighbors'
tab new

if `y'==2015 {
save matches, replace
}

if `y'>2015 {
append using matches
save matches, replace
}
}

tab year new

** See how premiums compare for new vs. existing
keep household_id year new `matchlist' 
merge 1:m household_id year using choice, keep(3) nogen
gen prem_subs=premium-subsidy

gen metal=0
replace metal=1 if strpos(plan_name,"BR")>0
replace metal=2 if strpos(plan_name,"SIL")>0
replace metal=3 if strpos(plan_name,"G")>0
replace metal=4 if strpos(plan_name,"_P")>0

gsort household_id year metal premium
by household_id year metal: gen rank=_n

preserve
keep if choice==1
collapse (mean) premium prem_subs, by(new) fast
gen year="Total"
reshape wide prem*, i(year) j(new)
gen diff=(premium1/premium0-1)*100
gen diff_subs=(prem_subs1/prem_subs0-1)*100
tempfile y
save `y'

restore, preserve
keep if choice==1
collapse (mean) premium prem_subs, by(new year) fast
reshape wide prem*, i(year) j(new)
label var premium0 "Existing"
label var premium1 "New"
label var prem_subs0 "Existing"
label var prem_subs1 "New"
gen diff=(premium1/premium0-1)*100
gen diff_subs=(prem_subs1/prem_subs0-1)*100
tostring year, replace force
append using `y'

order year premium0 premium1 diff prem_subs0 prem_subs1 diff_subs
foreach v of varlist prem* {
format `v' %8.2f
}
foreach v of varlist diff* {
format `v' %8.1f
}
list

listtex using "match_compare.tex", replace rstyle(tabular) ///
				head("\begin{tabular}{l c c c c c c}" ///
					"\toprule" ///
					"\\  & \multicolumn{3}{c}{Unsubsidized (\$)} & \multicolumn{3}{c}{Subsidized (\$)} \\" ///
					" Year & Existing  & New & Diff (\%) & Existing  & New & Diff (\%) \\ \midrule") ///
				foot(   "\bottomrule" ///
					"\end{tabular}")

clear
log close
exit
